// aixos 二次封装

import axios from "axios";
import config from "@/config";

const baseUrl =
  process.env.NODE_ENV === "development"
    ? config.baseUrl.dev
    : config.baseUrl.pro;

class HttpRequest {
  constructor(baseUrl) {
    this.baseUrl = baseUrl;
    this.queue = {}; //请求队列
  }
  // 配置
  getInsideConfig() {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        Authorization: "Bearer" + " " + window.sessionStorage.getItem("token"),
      },
    };
    return config;
  }
  // 拦截
  interceptors(instance, url) {
    // 拦截请求
    instance.interceptors.request.use((config) => {
      // 处理config
      return config;
    });
    // 拦截响应
    instance.interceptors.response.use(
      (res) => {
        // 处理响应
        return res;
      },
      (error) => {
        // 处理错误
        console.log(error);
        return { error: "发生错误" };
      }
    );
  }
  request(options) {
    const instance = axios.create(); //创建实例对象
    options = Object.assign(this.getInsideConfig(), options);
    this.interceptors(instance, options.url);
    return instance(options);
  }
}

const axiosObj = new HttpRequest(baseUrl);
export default axiosObj;
